package com.OnTimeTeam.OnTime;

import java.util.ArrayList;
import java.util.TreeSet;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//////////////////////
//The list adapter - whats makes the list to be arranged
public class SetSmartAlarmList extends ArrayAdapter<String> {

	private LayoutInflater inflater;
	
    private static final int TYPE_REGULAR_ITEM = 0;
    private static final int GENERAL_SETTINGS = 1;
    private static final int ALERT_SETTINGS = 2;
    private static final int VIBRATION_ITEM = 3;
    private static final int TRANSPORT_ITEM = 4;
    private static final int TYPE_MAX_COUNT = 5;
	
    private TreeSet<Integer> mSeparatorsSet1 = new TreeSet<Integer>();
    private TreeSet<Integer> mSeparatorsSet2 = new TreeSet<Integer>();
    private TreeSet<Integer> vibrationItems = new TreeSet<Integer>();
    private TreeSet<Integer> transportItems = new TreeSet<Integer>();
    
	private ArrayList<String> items; //all alarms details
	private SmartAlarmFeatures af;

	///////////////
	//CTOR - init' the array of AlarmFeatures and ApplicationContext object
	public SetSmartAlarmList(Context context, int textViewResourceId, ArrayList<String> items,SmartAlarmFeatures af) {
		super(context, textViewResourceId, items);

		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		//init' AlarmFeatures object
		this.af = af;
		
		this.items = new ArrayList<String>();
		for(int i = 0; i < items.size(); ++i){
			if(items.get(i).equals("GENERAL_SETTINGS"))
				addSeparatorItem1(items.get(i));
			else if(items.get(i).equals("ALERT_SETTINGS"))
				addSeparatorItem2(items.get(i));
			else if(items.get(i).equals("TYPE_REGULAR_ITEM"))
				addItem(items.get(i));
			else if(items.get(i).equals("VIBRATION_ITEM"))
				addVibItem(items.get(i));
			else if(items.get(i).equals("TRANSPORT_ITEM"))
				addTransportItem(items.get(i));
		}
	}


	public void addItem(final String item) {
    	items.add(item);
        notifyDataSetChanged();
    }
	
    public void addTransportItem(final String item) {
    	items.add(item);

    	transportItems.add(items.size() - 1);
        notifyDataSetChanged();
    }
    
    public void addVibItem(final String item) {
    	items.add(item);

    	vibrationItems.add(items.size() - 1);
        notifyDataSetChanged();
    }
	
    public void addSeparatorItem1(final String item) {
    	items.add(item);

    	mSeparatorsSet1.add(items.size() - 1);
        notifyDataSetChanged();
    }
    
    public void addSeparatorItem2(final String item) {
    	items.add(item);

    	mSeparatorsSet2.add(items.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
    	if(mSeparatorsSet1.contains(position))
    		return GENERAL_SETTINGS;
    	else if(mSeparatorsSet2.contains(position))
    		return ALERT_SETTINGS;
    	else if(vibrationItems.contains(position))
    		return VIBRATION_ITEM;
    	else if(transportItems.contains(position))
    		return TRANSPORT_ITEM;
        return TYPE_REGULAR_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public String getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
	
	
	////////////////////
	//This method sets the view of ONE item in the list (it gets it's index - it's the same index as the AlarmFeatures ArrayList)
	public View getView(final int index, View convertView, ViewGroup parent) {
	
    	TextViewHolder holder = null;
        int type = getItemViewType(index);

        if (convertView == null) {
        	
			switch (type) {
				case TYPE_REGULAR_ITEM:
	              	
	            	  holder = new SpecialViewHolder();
	                  convertView = inflater.inflate(R.layout.regular_alarm_item, null);
	                  holder.entry = (TextView)convertView.findViewById(R.id.entry);
	                  ((SpecialViewHolder)holder).details = (TextView)convertView.findViewById(R.id.details);

					break;
					
				case VIBRATION_ITEM:
	            	  holder = new VibrationItemViewHolder();
	                  convertView = inflater.inflate(R.layout.vibrate_item, null);
	                  holder.entry = (TextView)convertView.findViewById(R.id.entry);
	                  ((VibrationItemViewHolder)holder).image = (ImageView)convertView.findViewById(R.id.check);
	                  
					break;
				case TRANSPORT_ITEM:
	            	  holder = new TransportItemViewHolder();
	                  convertView = inflater.inflate(R.layout.vibrate_item, null);
	                  holder.entry = (TextView)convertView.findViewById(R.id.entry);
	                  ((TransportItemViewHolder)holder).image = (ImageView)convertView.findViewById(R.id.check);
					break;
	              case GENERAL_SETTINGS:
	              	
	            	  holder = new TextViewHolder();
	                  convertView = inflater.inflate(R.layout.lv_separator, null);
	                  holder.entry = (TextView)convertView.findViewById(R.id.choiceItem);
	                  holder.entry.setText(getContext().getResources().getString(R.string.generalSettings));
	                  break;
	              case ALERT_SETTINGS:

	            	  holder = new TextViewHolder();
	                  convertView = inflater.inflate(R.layout.lv_separator, null);
	                  holder.entry = (TextView)convertView.findViewById(R.id.choiceItem);
	                  holder.entry.setText(getContext().getResources().getString(R.string.alertSettings));
	              	break;
              	
			}
			
			convertView.setTag(holder);
		} 
		else {
			switch (type) {
			case TYPE_REGULAR_ITEM:
				holder = (SpecialViewHolder)convertView.getTag();
				break;
			case GENERAL_SETTINGS:
				holder = (TextViewHolder)convertView.getTag();
				break;
			case ALERT_SETTINGS:
				holder = (TextViewHolder)convertView.getTag();
				break;
			case VIBRATION_ITEM:
				holder = (VibrationItemViewHolder)convertView.getTag();
				break;
			case TRANSPORT_ITEM:
				holder = (TransportItemViewHolder)convertView.getTag();
				break;
			}
				
		}

			
		

		switch(index){
			case 1://Set time
				holder.entry.setText(getContext().getResources().getString(R.string.arrivalTime));
				((SpecialViewHolder)holder).details.setText(af.getArrivalHour() + ":" + af.getArrivalMinute());
				break;
			case 2://Set DepartureLocation
				holder.entry.setText(getContext().getResources().getString(R.string.origin));
				
				if(af.getOrigin().equals("") || af.getDest().equals("Use my current location"))
					((SpecialViewHolder)holder).details.setText(getContext().getResources().getString(R.string.currentLocation));
				else
					((SpecialViewHolder)holder).details.setText(af.getOrigin());
				
				break;
			case 3://Set DepartureLocation
				holder.entry.setText(getContext().getResources().getString(R.string.destination));
				
				if(af.getDest().equals("") || af.getDest().equals("Use my current location"))
					((SpecialViewHolder)holder).details.setText(getContext().getResources().getString(R.string.currentLocation));
				else
					((SpecialViewHolder)holder).details.setText(af.getDest());
				break;
			case 4:
				holder.entry.setText(getContext().getResources().getString(R.string.transportation));
				((TransportItemViewHolder)holder).image.setFocusable(false);
				
				if(af.getTransport().equals("Vehicle"))
					((TransportItemViewHolder)holder).image.setImageResource(R.drawable.vehicle);
				else if(af.getTransport().equals("Bicycle"))
					((TransportItemViewHolder)holder).image.setImageResource(R.drawable.bicycle);
				else if(af.getTransport().equals("Transit"))
					((TransportItemViewHolder)holder).image.setImageResource(R.drawable.bus);
				else
					((TransportItemViewHolder)holder).image.setImageResource(R.drawable.walk);
				
				break;
			case 5://Set DepartureLocation
				holder.entry.setText(getContext().getResources().getString(R.string.timeToOrganize));
				
				if( (af.getTTO().equals("")) || (af.getTTO().equals("0")) )
					((SpecialViewHolder)holder).details.setText(getContext().getResources().getString(R.string.noneIsNeeded));
				else
					((SpecialViewHolder)holder).details.setText(af.getTTO() + " " + getContext().getResources().getString(R.string.minutes));
				break;				
			case 6://Set repeat
				
				holder.entry.setText(getContext().getResources().getString(R.string.repeat));
				if(af.getDaysToRepeat().equals("Sun, Mon, Tue, Wed, Thu, Fri, Sat"))
					((SpecialViewHolder)holder).details.setText(getContext().getResources().getString(R.string.allWeek));
				else if(af.getDaysToRepeat().equals(""))
					((SpecialViewHolder)holder).details.setText(getContext().getResources().getString(R.string.none));
				else {
					
					String daysToRepeat = af.getDaysToRepeat();
					daysToRepeat = daysToRepeat.replace("Sun", getContext().getResources().getString(R.string.sun));
					daysToRepeat = daysToRepeat.replace("Mon", getContext().getResources().getString(R.string.mon));
					daysToRepeat = daysToRepeat.replace("Tue", getContext().getResources().getString(R.string.tue));
					daysToRepeat = daysToRepeat.replace("Wed", getContext().getResources().getString(R.string.wed));
					daysToRepeat = daysToRepeat.replace("Thu", getContext().getResources().getString(R.string.thu));
					daysToRepeat = daysToRepeat.replace("Fri", getContext().getResources().getString(R.string.fri));
					daysToRepeat = daysToRepeat.replace("Sat", getContext().getResources().getString(R.string.sat));
					
					((SpecialViewHolder)holder).details.setText(daysToRepeat);
					
				}
				break;
			case 7://Set label
				holder.entry.setText(getContext().getResources().getString(R.string.label));
				if(!af.getLabel().equals(""))
					((SpecialViewHolder)holder).details.setText(af.getLabel());
				else
					((SpecialViewHolder)holder).details.setText(getContext().getResources().getString(R.string.labelHint));
				
				break;		
			case 8://Set snooze
				holder.entry.setText(getContext().getResources().getString(R.string.snooze));
				if(af.getSnoozeRepeat().equals("1"))
					((SpecialViewHolder)holder).details.setText(getContext().getResources().getString(R.string.none));
				else {
					
					String snoozeInterval = null;
					String snoozeRepeat = null;
					
					if(af.getSnoozeRepeat().equals("1"))
						snoozeRepeat = getContext().getResources().getString(R.string.none);
					else if(af.getSnoozeRepeat().equals("3"))
						snoozeRepeat = getContext().getResources().getString(R.string.repeat3Times);
					else if(af.getSnoozeRepeat().equals("4"))
						snoozeRepeat = getContext().getResources().getString(R.string.repeat4Times);
					else if(af.getSnoozeRepeat().equals("5"))
						snoozeRepeat = getContext().getResources().getString(R.string.repeat5Times);
					else if(af.getSnoozeRepeat().equals("6"))
						snoozeRepeat = getContext().getResources().getString(R.string.repeat6Times);
					else if(af.getSnoozeRepeat().equals("10"))
						snoozeRepeat = getContext().getResources().getString(R.string.repeat10Times);
					else if(af.getSnoozeRepeat().equals("Unlimited"))
						snoozeRepeat = getContext().getResources().getString(R.string.unlimited);
					
					
					if(af.getSnoozeInterval().equals("3"))
						snoozeInterval = getContext().getResources().getString(R.string.fireEvery3Minutes);
					else if(af.getSnoozeInterval().equals("5"))
						snoozeInterval = getContext().getResources().getString(R.string.fireEvery5Minutes);
					else if(af.getSnoozeInterval().equals("10"))
						snoozeInterval = getContext().getResources().getString(R.string.fireEvery10Minutes);
					else if(af.getSnoozeInterval().equals("15"))
						snoozeInterval = getContext().getResources().getString(R.string.fireEvery15Minutes);
					else if(af.getSnoozeInterval().equals("20"))
						snoozeInterval = getContext().getResources().getString(R.string.fireEvery20Minutes);					
					else if(af.getSnoozeInterval().equals("30"))
						snoozeInterval = getContext().getResources().getString(R.string.fireEvery30Minutes);
					
					if(af.getSnoozeRepeat().equals("1"))
						((SpecialViewHolder)holder).details.setText(snoozeRepeat);
					else
						((SpecialViewHolder)holder).details.setText(snoozeRepeat + "/ " + snoozeInterval);

				}				
				break;		
			case 10:
				holder.entry.setText(getContext().getResources().getString(R.string.tune));
				
				String tuneName = "Alarm ";
				
				if(af.getTune().equals("0"))
					tuneName = tuneName + "I";
				else if(af.getTune().equals("1"))
					tuneName = tuneName + "II";
				else if(af.getTune().equals("2"))
					tuneName = tuneName + "III";
				else if(af.getTune().equals("3"))
					tuneName = tuneName + "IV";				
				else if(af.getTune().equals("4"))
					tuneName = tuneName + "V";
				else if(af.getTune().equals("5"))
					tuneName = tuneName + "VI";				
				else if(af.getTune().equals("6"))
					tuneName = tuneName + "VII";
				else
					tuneName = af.getTune();
				
				((SpecialViewHolder)holder).details.setText(tuneName.substring(af.getTune().lastIndexOf("/")+1));
				break;
			case 11:
				holder.entry.setText(getContext().getResources().getString(R.string.volume));
				if(af.getVolume().equals("0"))
					((SpecialViewHolder)holder).details.setText(getContext().getResources().getString(R.string.silentAlarm));
				else
					((SpecialViewHolder)holder).details.setText(af.getVolume() + "%");		
				break;
			case 12:				
				holder.entry.setText(getContext().getResources().getString(R.string.vibration));
				((VibrationItemViewHolder)holder).image.setColorFilter(Color.argb(0, 0, 0, 0));

				//Log.e("VIB", af.getVibration());
				
				((VibrationItemViewHolder)holder).image.setFocusable(false);
				if(!af.getVibration().equals("None")){
					((VibrationItemViewHolder)holder).image.setColorFilter(Color.argb(255, 0, 153, 204));
				}
				else{
					((VibrationItemViewHolder)holder).image.setImageResource(R.drawable.vibrate_off);
				}
				
				
				/*if(af.getVibration().equals("None"))
					details.setText(getContext().getResources().getString(R.string.none));
				else if(af.getVibration().equals("Standard I"))
					details.setText(getContext().getResources().getString(R.string.vibStandard1));
				else if(af.getVibration().equals("Standard II"))
					details.setText(getContext().getResources().getString(R.string.vibStandard2));
				else if(af.getVibration().equals("Fast"))
					details.setText(getContext().getResources().getString(R.string.vibFast));				
				else if(af.getVibration().equals("Intensive"))
					details.setText(getContext().getResources().getString(R.string.vibIntensive));				
				else if(af.getVibration().equals("3 Bursts"))
					details.setText(getContext().getResources().getString(R.string.vib3Busrts));
				else if(af.getVibration().equals("Morse SOS"))
					details.setText(getContext().getResources().getString(R.string.vibMorse));*/
						
				break;	
		}//end cases
		

		
		//return the view of the item
		return convertView;
	
	}//end of method 'getView'

    public class TextViewHolder{
    	public TextView entry;
    }

    public class VibrationItemViewHolder extends TextViewHolder{
    	public ImageView image;
    }
    
    public class TransportItemViewHolder extends TextViewHolder{
    	public ImageView image;
    }

    public class SpecialViewHolder extends TextViewHolder{
    	public TextView details;
    }
    
    
	
}//EOF